﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.Unity;
using ePlanet.ArchiveMange.Models;
using ePlanet.ArchiveMange.Services;
using ePlanet.ArchiveBorrow.Models;

namespace Web.Views.ArchiveBorrow.ArchiveBorrowView
{
    public partial class DocumentView : ArchiveBorrowViewBase
    {
        [Dependency]
        public IDocumentArchiveService m_IDocumentArchiveService
        {
            get;
            set;
        }

        [Dependency]
        public IArchiveMenuService ArchiveMenuService
        {
            get;
            set;
        }

        [Dependency]
        public Web.SystemConfig.Services.ICodeTable CodeTableService
        {
            get;
            set;
        }

        public string archiveTypeName;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["DataID"]))
                {
                    Guid dataID = new Guid(Request.QueryString["DataID"]);
                    this.InitArchiveBorrowModel(dataID);
                    if (this.m_CurrentArchiveBorrowModel != null)
                    {
                        ArchiveBorrowView masterPage = (ArchiveBorrowView)this.Master;
                        masterPage.CurrentArchiveBorrow = this.m_CurrentArchiveBorrowModel;
                        ArchiveMangeModal.DOCUMENT_ARCHIVERow currentArchiveData = this.GetDocumentArchive(this.m_CurrentArchiveBorrowModel.ARCHIVEDATAID);
                        this.BindData(currentArchiveData);
                    }
                    else
                    {
                        Alert("获取借阅对象失败！");
                    }
                }
                else
                {
                    Alert("参数DataID不能为空！");
                }
            }
        }

        #region 私有方法
        private void BindData(ArchiveMangeModal.DOCUMENT_ARCHIVERow currentArchiveData)
        {
            this.BindDocumentArchive(currentArchiveData);
        }

        /// <summary>
        /// 绑定页面控件数据
        /// </summary>
        /// <param name="currentArchiveData"></param>
        private void BindDocumentArchive(ArchiveMangeModal.DOCUMENT_ARCHIVERow currentArchiveData)
        {
            //throw new NotImplementedException();
            this.txtArchiveNum.Text = currentArchiveData.ARCHIVENUM;
            this.txtArchiveSource.Text = currentArchiveData.ARCHIVESOURCE;
            ArchiveMangeModal.ARCHIVEMENUDataTable dtMenu = ArchiveMenuService.GetArchiveMenuById(currentArchiveData.ARCHIVETYPE.ToString());
            ArchiveMangeModal.ARCHIVEMENURow drMenu = dtMenu.Rows[0] as ArchiveMangeModal.ARCHIVEMENURow;
            this.archiveTypeName = drMenu.ARCHIVEMENU;
            this.txtArchiveType.Text = drMenu.ARCHIVEMENU;
            //this.h_ArchiveTypeId.Value = currentArchiveData.ARCHIVETYPE.ToString();
            this.txtBoxNum.Text = currentArchiveData.BOXNUM;
            this.txtDoplicateBodyNum.Text = currentArchiveData.DOPLICATEBODYNUM.ToString();
            this.txtFileNum.Text = currentArchiveData.FILENUM;
            //DataTable dtOrg = orgService.GetORGInfo(currentArchiveData.FILEORGID);
            //this.txtFileOrgName.Text = dtOrg.Rows[0]["Org_Name"].ToString();
            //this.h_FileOrgID.Value = currentArchiveData.FILEORGID;
            this.txtFileOrgName.Text = currentArchiveData.FILEORGID;
            this.txtFullFondsNum.Text = currentArchiveData.FULLFONDSNUM;
            this.txtMainBodyNum.Text = currentArchiveData.MAINBODYNUM.ToString();
            this.txtPageNum.Text = currentArchiveData.PAGENUM.ToString();
            this.txtPublisher.Text = currentArchiveData.PUBLISHER;
            this.txtPublishTime.Text = currentArchiveData.PUBLISHTIME.ToString("yyyy-MM-dd");
            this.txtRemark.Text = currentArchiveData.REMARK;
            this.txtStorageType.Text = currentArchiveData.IsSTORAGETYPENull() ? string.Empty : currentArchiveData.STORAGETYPE;
            this.txtSummary.Text = currentArchiveData.SUMMARY;
            this.txtTempOrderNum.Text = currentArchiveData.TEMPORDERNUM;
            this.txtTitle.Text = currentArchiveData.TITLE;
            this.txtCarrierType.Text = CodeTableService.GetpkNameByCodepkValue(currentArchiveData.F_CARRIERTYPE);
            this.txtKeepTime.Text = CodeTableService.GetpkNameByCodepkValue(currentArchiveData.F_KEEPTIME);
            this.txtSecurity.Text = CodeTableService.GetpkNameByCodepkValue(currentArchiveData.F_SECURITYTYPE);
            this.txtKeepSecurityTime.Text = CodeTableService.GetpkNameByCodepkValue(currentArchiveData.F_KEEPSECURITYTIME);


        }

        private ArchiveMangeModal.DOCUMENT_ARCHIVERow GetDocumentArchive(string dataID)
        {
            ArchiveMangeModal.DOCUMENT_ARCHIVEDataTable oDataTable = this.m_IDocumentArchiveService.GetAllDocumenArchivetDataById(dataID);
            if (oDataTable != null && oDataTable.Rows.Count > 0)
            {
                return oDataTable.Rows[0] as ArchiveMangeModal.DOCUMENT_ARCHIVERow;
            }
            else
            {
                return null;
            }
        }
        #endregion
    }
}